$(function(){
	if(!$_GET['page']) $_GET['page'] = 1;
	if(!$_GET['limit']) $_GET['limit'] = 0;
	Table = $('#blacklist-list-div>table');
	Tablist = $('ul[role="tablist"]');
	$.getBlacklist = function(arg){
		var url = SITE_URL+'mod.php?blacklist::';
		if(!arg['blacklist']){
			url += 'getMulti';
		}else if(arg['blacklist'] == 'uid'){
			url += 'getUsers';
		}else if(arg['blacklist'] == 'email'){
			url += 'getEmails';
		}else if(arg['blacklist'] == 'ip'){
			url += 'getIps';
		}
		$.ajax({
			url: url,
			success: function(result){
				var data = result.data,
					th = '<th>#</th>',
					tr = tr2 = '',
					i = 1 + ($_GET['page'] - 1) * ($_GET['limit'] || 10);
				if(!arg['blacklist']){
					th += '<th>用户</th><th>Email 地址</th><th>IP 地址</th>';
				}else if(arg['blacklist'] == 'uid'){
					th += '<th>用户名/昵称</th>'
				}else if(arg['blacklist'] == 'email'){
					th += '<th>Email 地址</th>';
				}else if(arg['blacklist'] == 'ip'){
					th += '<th>IP 地址</th>';
				}
				th += '<th>操作</th>';
				if(arg['blacklist'] == 'email' || arg['blacklist'] == 'ip'){
					var email = arg['blacklist'] == 'email';
					tr2 += '<tr><td><label for="new">添加</td><td><input id="new" type="'+(email ? 'email' : 'text')+'" name="blacklist_'+arg['blacklist']+'" placeholder="请输入'+(email ? '邮件' : ' IP ')+'地址"/></td><td><span title="加入黑名单"" class="add"><i class="glyphicon glyphicon-plus"></i></span></td></tr>';
				}
				if(result.success){
					for(var x in data){
						tr += '<tr data-id="'+data[x]['blacklist_id']+'"><td>'+i+'</td>';
						if(!arg['blacklist']){
							tr += '<td>';
							if(data[x]['blacklist_user']){
								tr += '<a href="'+ADMIN_URL+'users.html?action=update&user_id='+data[x]['blacklist_uid']+'">'+data[x]['blacklist_user']+'</a>';
							}
							tr += '</td><td>'+data[x]['blacklist_email']+'</td><td>'+data[x]['blacklist_ip']+'</td>';
						}else if(arg['blacklist'] == 'uid'){
							tr += '<td><a href="'+ADMIN_URL+'users.html?action=update&user_id='+data[x]['blacklist_uid']+'">'+data[x]['blacklist_user']+'</a></td>';
						}else if(arg['blacklist'] == 'email'){
							tr += '<td>'+data[x]['blacklist_email']+'</td>';
						}else if(arg['blacklist'] == 'ip'){
							tr += '<td>'+data[x]['blacklist_ip']+'</td>';
						}
						tr += '<td><span title="移出黑名单" class="delete"><i class="glyphicon glyphicon-remove"></i></span></td></tr>';
						i++;
					}
				}
				Table.html('<thead><tr>'+th+'</tr></thead><tbody>'+tr+tr2+'</tbody>').find('input[name]').focus();
				var target = Tablist.find('li.active').find('a'),
					text = target.text().replace(/\(.*\)/, '('+(result.total || 0)+')');
				target.text(text);
			},
			error: function(xhr){
				alert('与服务器的连接出现错误，数据获取失败。');
				console.log(xhr.responseText);
			}
		});
	}
	Tablist.find('a').click(function(event){
		event.preventDefault();
		$(this).parent('li').addClass('active').siblings('li').removeClass('active');
		var arg = {'blacklist': $(this).data('blacklist')};
		$_GET['page'] = 1;
		$.extend(arg, $_GET);
		$.getBlacklist(arg);
	});
	if(!$_GET['blacklist']){
		Tablist.find('a').eq(0).click();
	}
	Table.on('click', 'tbody span.delete', function(event){
		if(confirm('即将永久删除所选项目，确认？')){
			var target = $(this).closest('tr');
			$.ajax({
				url: SITE_URL+'mod.php?blacklist::delete|blacklist_id:'+target.data('id'),
				success: function(result){
					alert(result.data);
					if(result.success){
						Tablist.find('li.active').find('a').click();
					}
				},
				error: function(xhr){
					alert('与服务器的连接出现错误，记录删除失败。');
					console.log(xhr.responseText);
				}
			});
		}
	});
	Table.on('click', 'tbody span.add', function(event){
		var target = $(this).closest('tr').find('input[name]'),
			value = target.val(),
			key = target.attr('name');
		if(key == 'blacklist_email' && !value.match(/[\S]+\@[\S]+\.[\S]+/)){
			alert('请填写一个合法的邮箱。');
			return false;
		}else if(key == 'blacklist_ip' && !value.match(/[0-9]+.[0-9]+.[0-9]+.[0-9]+/)){
			alert('请填写一个合法的 IP 地址。');
			return false;
		}
		data = {};
		data[key] = value;
		$.ajax({
			url: SITE_URL+'mod.php?blacklist::add',
			type: 'post',
			data: data,
			success: function(result){
				if(result.success){
					alert('添加成功。');
					Tablist.find('li.active').find('a').click();
				}else{
					alert(result.data);
				}
			},
			error: function(xhr){
				alert('与服务器的连接出现错误，记录添加失败。');
				console.log(xhr.responseText);
			}
		});
	});

	/** 显示帮助信息 */
	$.showHelp('黑名单中的用户将会被禁止进行评论等操作。');
})